﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tradex.Messaging {

    /// <summary>
    /// This struct defines the address of a port. It is only used internally do define ports.
    /// </summary>
    [Serializable]
    public struct Address : IComparable<Address> {

        ///// <summary>
        ///// This 32 bit number defines the bus the message is to be sent to. It is here for a possible later expansion into a client/server
        ///// architecture (where every client will act as it's own bus). A value of 0 (the default value) means "local". A value of Int32.MaxValue
        ///// indicaqtes a broadcast address, which should be distributed to all bus systems.
        ///// </summary>
        //internal Int32 BusId { get; set; }

        /// <summary>
        /// This 32 bit number defines the Id of the port. It carries no particular meaning... if an address of a given service is needed
        /// or wanted, a port user should request the currently valid address of aservice from the bus, given the determined service code.
        /// </summary>
        internal UInt32 PortId { get; set; }

        #region IComparable<Address> Members

        /// <summary>
        /// Compares one address to another address.
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public int CompareTo(Address other) {
            return this.PortId.CompareTo(other.PortId);
        }

        #endregion

        /// <summary>
        /// Checks whether the port is a the port.
        /// </summary>
        /// <returns></returns>
        public bool IsBroadcast() {
            return PortId == UInt32.MaxValue;
        }

        /// <inheritdoc />
        public override bool Equals(object obj) {
            if (!(obj is Address)) {
                return false;
            }
            return this.CompareTo((Address)obj) == 0;
        }

        /// <inheritdoc />
        public override int GetHashCode() {
            return PortId.GetHashCode();
        }

        /// <inheritdoc />
        public static bool operator ==(Address left, Address right) {
            return left.PortId == right.PortId;
        }

        /// <inheritdoc />
        public static bool operator !=(Address left, Address right) {
            return left.PortId != right.PortId;
        }

        /// <inheritdoc />
        public static bool operator <(Address left, Address right) {
            if (left.CompareTo(right) == -1) {
                return true;
            }
            return false;
        }

        /// <inheritdoc />
        public static bool operator >(Address left, Address right) {
            if (left.CompareTo(right) == +1) {
                return true;
            }
            return false;
        }

        /// <summary>
        /// This predefined static address defines the address to be used for a local broadcast (that is sent to all
        ///  ports connected to the bus the message is passing through).
        /// </summary>
        public static readonly Address LocalBroadcast = new Address() { PortId = UInt32.MaxValue };
    }

}
